using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Nop.Core.Configuration;
using Nop.Core.Data;
using Nop.Core.Domain.WindAlert;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Data;
using Nop.Data.WindAlert;
using Nop.Services.Media;
using Nop.Services.Orders;
using Nop.Services.Vendors;
using Nop.Services.WindAlert;
using System;
using System.IO;
using System.Web.Hosting;


namespace Nop.Web.WindAlert
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        protected virtual string MapPath(string path)
        {
            if (HostingEnvironment.IsHosted)
            {
                //hosted
                return HostingEnvironment.MapPath(path);
            }
            else
            {
                //not hosted. For example, run in unit tests
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
                return Path.Combine(baseDirectory, path);
            }
        }

        // <summary>
        /// Register services and interfaces
        /// </summary>
        /// <param name="builder">Container builder</param>
        /// <param name="typeFinder">Type finder</param>
        /// <param name="config">Config</param>
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        { 
            //windalert
            builder.RegisterType<SpotService>().As<ISpotService>().InstancePerHttpRequest();
            builder.RegisterType<CameraService>().As<ICameraService>().InstancePerHttpRequest();
            builder.RegisterType<WindHelpers>().As<IWindHelpers>().InstancePerHttpRequest();
            builder.RegisterType<WindDataService>().As<IWindDataService>().InstancePerHttpRequest();
            builder.RegisterType<WaveDataService>().As<IWaveDataService>().InstancePerHttpRequest();
            builder.RegisterType<ForecastDataService>().As<IForecastDataService>().InstancePerHttpRequest();
            builder.RegisterType<WindAlertPictureService>().As<IWindAlertPictureService>().InstancePerHttpRequest();
            builder.RegisterType<AlertService>().As<IAlertService>().InstancePerHttpRequest();
            builder.RegisterType<MobileDeviceService>().As<IMobileDeviceService>().InstancePerHttpRequest();

            builder.RegisterType<WOrderService>().As<IWOrderService>().InstancePerHttpRequest();
            
            #region Data

            //data layer
            var dataSettingsManager = new DataSettingsManager();
            var dataProviderSettings = dataSettingsManager.LoadSettings(Path.Combine(MapPath("~/App_Data/"), "windalert.txt"));

            if (dataProviderSettings != null && dataProviderSettings.IsValid())
            {
                //register named context
                builder.Register<IDbContext>(c => new WindAlertObjectContext(dataProviderSettings.DataConnectionString))
                    .Named<IDbContext>("nop_object_context_windalert")
                    .InstancePerHttpRequest();

                builder.Register<WindAlertObjectContext>(c => new WindAlertObjectContext(dataProviderSettings.DataConnectionString))
                    .InstancePerHttpRequest();
            }
            else
            {
                //register named context
                builder.Register<IDbContext>(c => new WindAlertObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                    .Named<IDbContext>("nop_object_context_windalert")
                    .InstancePerHttpRequest();

                builder.Register<WindAlertObjectContext>(c => new WindAlertObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                    .InstancePerHttpRequest();
            }

            //override required repository with our custom context
            builder.RegisterType<EfRepository<WindData>>()
                .As<IRepository<WindData>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_windalert"))
                .InstancePerHttpRequest();
            builder.RegisterType<EfRepository<WaveData>>()
                .As<IRepository<WaveData>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_windalert"))
                .InstancePerHttpRequest();
            builder.RegisterType<EfRepository<ForecastData>>()
               .As<IRepository<ForecastData>>()
               .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_windalert"))
               .InstancePerHttpRequest();
            #endregion
        }

        public int Order
        {
            get { return 1; }
        }
    }
}
